package day8;

import java.io.*;
import java.net.Socket;

/**
 * @author oldliu
 * @since 1.0
 */
//客户端发文件名，我把文件发给他
public class FileThread extends Thread {
    private final Socket s;

    public FileThread(Socket s ){
        this.s=s;
    }

    public static final String folder="E:\\java_workspace\\hbuilderproj\\java82pre\\java82pre\\image";
    @Override
    public void run() {
        System.out.println(s + "连接了");
        try {
            InputStream in = s.getInputStream();
            OutputStream out = s.getOutputStream();
            //
            BufferedReader br=new BufferedReader(new InputStreamReader(in));
            String fileName=br.readLine();
            //拼上路径
            fileName=folder+"\\"+fileName;
            FileInputStream fin=new FileInputStream(fileName);
            byte []b=new byte[1024];
            int len=0;
            //从文件中读，往网络中写
            while ((len=fin.read(b))!=-1){
                out.write(b,0,len);
            }
            out.flush();
            fin.close();
            System.out.println("发送 "+fileName+"给"+s+" 完成");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                s.close();
            } catch (Exception e) {

            }
        }
    }
}
